package net.zoneland.greenleaf.timer.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import net.zoneland.greenleaf.common.pagination.Pagination;
import net.zoneland.greenleaf.system.controller.BaseController;
import net.zoneland.greenleaf.timer.entity.Log_timer_runlog;
import net.zoneland.greenleaf.timer.service.Log_timer_runlogServiceI;

/**
 * LOG_TIMER_RUNLOG[定时代理运行日志]表操作控制层（Controller|Action）

 * 表   名：LOG_TIMER_RUNLOGCONTROLLER<br/>
 * 注   释：<br/>
 * 作   者：李义<br/>
 * 日   期：2014-09-03 20:35:13
**/
@Controller
@RequestMapping("log_timer_runlog")
public class Log_timer_runlogController extends BaseController {
	/**
	 * 如果需要系统对此Controller的访问进行监控，记录服务访问的时间，耗时，那么请将monitor属性设置为“true”
	 */
	public boolean monitor = false; 
	
	public static Logger logger = Logger.getLogger(Log_timer_runlogController.class);
	//获取类的名称
	private String clazzName = new Object() {
		public String getClassName() {
			String clazzName = this.getClass().getName();
			return clazzName.substring(0, clazzName.lastIndexOf('$'));
		}
	}.getClassName();
  
	@Autowired
	private Log_timer_runlogServiceI log_timer_runlogService;
	
	/**
     * 根据ID获取信息数据
     * 
     * @param id
     * @return
     */
    @RequestMapping( method = RequestMethod.GET, value = "/{id}", produces = "text/html;charset=UTF-8" )
    @ResponseBody
    public Object get( @PathVariable("id") String id ){
		Map<String, Object> map = new HashMap<String, Object>();
		Log_timer_runlog entity = null;
		Boolean check = true;
		
		map.put("id", id);
		if( id == null || "(0)".equals( id ) || id.isEmpty() ){
			check = false;
			map.put("result", "error");
			map.put("message", "系统获取到的ID不正常，ID:" + id );
		}
		if( check ){
			try {
				entity = log_timer_runlogService.get( map );
			} catch (Exception e) {
				check = false;
				map.put("result", "error");
				map.put("message", "系统根据ID获取对象失败!");
				logger.error("系统根据ID获取对象发生异常。ID:" + id, e);
			}
		}
		if( check ){
			if( entity != null ){
				map.put("result", "success");
				map.put( "data", entity );
			}else{
				map.put("result", "error");
				map.put("message", "数据不存在！ID:" + id );
			}
		}
		return map;
	}
    
    /**
	 * 获取信息表列表
	 * 返回JSON格式数据
	 * 如果不需要分页，您可以考虑将pagesize设置为足够大的数字，系统默认为20
	 * 
	 * @param Map<String, Object> paramMap 
	**/
	@RequestMapping( method = RequestMethod.POST, value = "/list" )
    @ResponseBody
	public Object list( @RequestBody Map<String, Object> paramMap ) {		
		String page = "1";
		Integer pagesize = 20, totalRecord=0;
		List<Log_timer_runlog> list  = null;
		Map<String, Object> map=new HashMap<String,Object>();
		Pagination<?> pagination = null;
		Boolean check = true;
		
		if( paramMap == null ){
			check = false;
			map.put("result", "error");
			map.put("message", "参数列表为空，无法进行数据查询！");
		}
		if( check ){
			if(paramMap.get("page")!=null){
				page = paramMap.get("page").toString();
			}
			if(paramMap.get("pagesize") != null){
				try{
					pagesize = Integer.parseInt(paramMap.get("pagesize").toString());
				}catch(Exception e){
					pagesize = 20;
				}
			}
			if( paramMap.get("c_codes")   != null && paramMap.get("c_codes").toString().trim().length() > 1){
				paramMap.put("c_codes", paramMap.get("c_codes").toString().trim().split(","));
			}
		}
		if( check ){
			try{
				totalRecord = log_timer_runlogService.getListCount( paramMap );
			}catch( Exception e ){
				check = false;
				map.put("result", "error");
				map.put("message", "系统根据条件查询信息数据条目总数时发生异常！");
				logger.error( e );
			}
		}
		if( check ){
			try{
				pagination = Pagination.newPagination( pagesize, page, totalRecord );
				paramMap.put( "startRow", pagination.getStartRow() );
				paramMap.put( "pageSize", pagesize );
				paramMap.put("orderColName", "C_ORDER");
			}catch( Exception e ){
				check = false;
				map.put("result", "error");
				map.put("message", "系统根据条件进行分页信息计算时发生异常！");
				logger.error( e );
			}
		}
		if( check ){		
			if( totalRecord>0 ){
				try{
					list = log_timer_runlogService.getList( paramMap );
				}catch( Exception e ){
					check = false;
					map.put("result", "error");
					map.put("message", "系统根据条件进行信息列表查询信息数据时发生异常！");
					logger.error( e );
				}
			}
		}
		if( check ){
			if( list == null ){
				list = new ArrayList<Log_timer_runlog>();
			}
		}
		if( check ){
			map.put( "result", "success" );
			map.put( "message", "系统查询信息数据成功！" );
			map.put( "pagination", pagination );
			map.put( "data", list );
		}
		return map;
	}

	/**
	 * 根据ID删除一个系统配置信息表信息
	 * 返回JSON格式数据
	 * @param int id
	**/
	@RequestMapping( method = RequestMethod.DELETE, value = "/{id}" )
    @ResponseBody
    public Object deleteById( @PathVariable("id") String id ){
		Map<String, Object> map = new HashMap<String, Object>();
		Log_timer_runlog entity = null;
		Boolean check = true;
		
		map.put("id", id);
		if( id == null || id.isEmpty() || "id".equalsIgnoreCase( id )){
			check = false;
			map.put( "result", "error" );
			map.put( "message", "需要删除的信息ID为空!" );
		}
		if( check ){
			try {
				entity = log_timer_runlogService.get(map);
				if( entity == null ){
					check = false;
					map.put("result", "error");
					map.put("message", "需要删除的信息数据不存在失败!ID:" + id );
				}
			} catch (Exception e) {
				check = false;
				map.put("result", "error");
				map.put("message", "根据ID查询需要删除的信息数据失败!ID:" + id );
				logger.error("根据ID查询需要删除的信息数据失败!ID:" + id, e);
			}
		}
		if( check ){
			try {
				log_timer_runlogService.delete( map );
				map.put( "result", "success" );
				map.put( "message", "成功删除一个信息数据！" );
			} catch (Exception e) {
				map.put("result", "error");
				map.put("message", "删除信息数据失败!ID:" + id );
				logger.error("删除信息数据失败!ID:" + id, e);
			}
		}
		return map;
	}
	
	/**
	 * 根据ID删除多个系统配置信息表信息
	 * 返回JSON格式数据
	 * @param Map<String, Object> paramMap
	**/
	@RequestMapping( method = RequestMethod.POST, value = "/delete/batch" )
    @ResponseBody
	public Object deleteByIds( @RequestBody Map<String, Object> paramMap ){
		Map<String, Object> map = new HashMap<String, Object>();
		String ids_str = null;
		Boolean check = true;
		
		if( paramMap == null || paramMap.get( "ids" ) == null ){
			check = false;
			map.put("result", "error");
			map.put("message", "参数列表为空，无法进行数据删除！");
		}
		if( check ){
			ids_str = (String)paramMap.get( "ids" );
			if( ids_str != null && ids_str.isEmpty() ){
				check = false;
				map.put("result", "error");
				map.put("message", "参数列表中ID列表为空，无法进行数据删除！");
			}else{
				map.put("ids", ids_str.split(",") );
			}
		}
		if( check ){
			try {
				log_timer_runlogService.delete( map );
				map.put("result", "success");
				map.put("message", "成功删除信息数据！");
			} catch (Exception e) {
				map.put( "result", "error");
				map.put( "message", "删除信息数据失败!ID:" + paramMap.get( "ids" ) );
				logger.error( "系统信息数据失败!ID:" + paramMap.get( "ids" ), e );
			}
		}
		return map;
	}
}
